
/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */


#ifndef _NGX_HTTP_UPSTREAM_H_INCLUDED_
#define _NGX_HTTP_UPSTREAM_H_INCLUDED_


#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
#include <ngx_event_connect.h>
#include <ngx_event_pipe.h>
#include <ngx_http.h>


#define NGX_HTTP_UPSTREAM_FT_ERROR          0x00000002
#define NGX_HTTP_UPSTREAM_FT_TIMEOUT        0x00000004
#define NGX_HTTP_UPSTREAM_FT_INVALID_HEADER 0x00000008
#define NGX_HTTP_UPSTREAM_FT_HTTP_500       0x00000010
#define NGX_HTTP_UPSTREAM_FT_HTTP_502       0x00000020
#define NGX_HTTP_UPSTREAM_FT_HTTP_503       0x00000040
#define NGX_HTTP_UPSTREAM_FT_HTTP_504       0x00000080
#define NGX_HTTP_UPSTREAM_FT_HTTP_403       0x00000100
#define NGX_HTTP_UPSTREAM_FT_HTTP_404       0x00000200
#define NGX_HTTP_UPSTREAM_FT_HTTP_429       0x00000400
#define NGX_HTTP_UPSTREAM_FT_UPDATING       0x00000800
#define NGX_HTTP_UPSTREAM_FT_BUSY_LOCK      0x00001000
#define NGX_HTTP_UPSTREAM_FT_MAX_WAITING    0x00002000
#define NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT 0x00004000
#define NGX_HTTP_UPSTREAM_FT_NOLIVE         0x40000000
#define NGX_HTTP_UPSTREAM_FT_OFF            0x80000000

#define NGX_HTTP_UPSTREAM_FT_STATUS                                  \
    (NGX_HTTP_UPSTREAM_FT_HTTP_500 | NGX_HTTP_UPSTREAM_FT_HTTP_502   \
     | NGX_HTTP_UPSTREAM_FT_HTTP_503 | NGX_HTTP_UPSTREAM_FT_HTTP_504 \
     | NGX_HTTP_UPSTREAM_FT_HTTP_403 | NGX_HTTP_UPSTREAM_FT_HTTP_404 \
     | NGX_HTTP_UPSTREAM_FT_HTTP_429)

#define NGX_HTTP_UPSTREAM_INVALID_HEADER 40


#define NGX_HTTP_UPSTREAM_IGN_XA_REDIRECT   0x00000002
#define NGX_HTTP_UPSTREAM_IGN_XA_EXPIRES    0x00000004
#define NGX_HTTP_UPSTREAM_IGN_EXPIRES       0x00000008
#define NGX_HTTP_UPSTREAM_IGN_CACHE_CONTROL 0x00000010
#define NGX_HTTP_UPSTREAM_IGN_SET_COOKIE    0x00000020
#define NGX_HTTP_UPSTREAM_IGN_XA_LIMIT_RATE 0x00000040
#define NGX_HTTP_UPSTREAM_IGN_XA_BUFFERING  0x00000080
#define NGX_HTTP_UPSTREAM_IGN_XA_CHARSET    0x00000100
#define NGX_HTTP_UPSTREAM_IGN_VARY          0x00000200


typedef struct
{
    ngx_uint_t status;
    ngx_msec_t response_time;
    ngx_msec_t connect_time;
    ngx_msec_t header_time;
    ngx_msec_t queue_time;
    off_t      response_length;
    off_t      bytes_received;
    off_t      bytes_sent;

    ngx_str_t *peer;
} ngx_http_upstream_state_t;


typedef struct
{
    ngx_hash_t  headers_in_hash;
    ngx_array_t upstreams;
    /* ngx_http_upstream_srv_conf_t */
#if (NGX_HTTP_UPSTREAM_RBTREE)

    ngx_list_t implicit_upstreams;
    /* ngx_http_upstream_srv_conf_t */

    ngx_rbtree_t      rbtree;
    ngx_rbtree_node_t sentinel;

#endif
} ngx_http_upstream_main_conf_t;

typedef struct ngx_http_upstream_srv_conf_s ngx_http_upstream_srv_conf_t;

typedef ngx_int_t (*ngx_http_upstream_init_pt)(
    ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us);
typedef ngx_int_t (*ngx_http_upstream_init_peer_pt)(
    ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *us);


typedef struct
{
    ngx_http_upstream_init_pt      init_upstream;
    ngx_http_upstream_init_peer_pt init;
    void                          *data;
} ngx_http_upstream_peer_t;


typedef struct
{
    ngx_str_t   name;
    ngx_addr_t *addrs;
    ngx_uint_t  naddrs;
    ngx_uint_t  weight;
    ngx_uint_t  max_conns;
    ngx_uint_t  max_fails;
    time_t      fail_timeout;
    ngx_msec_t  slow_start;
    ngx_uint_t  down;
#if (T_NGX_HTTP_UPSTREAM_ID)
    ngx_str_t id;
#endif

#if (T_NGX_HTTP_DYNAMIC_RESOLVE)
    ngx_str_t host;
#endif

    unsigned backup : 1;

    NGX_COMPAT_BEGIN(6)
    NGX_COMPAT_END
} ngx_http_upstream_server_t;


#define NGX_HTTP_UPSTREAM_CREATE       0x0001
#define NGX_HTTP_UPSTREAM_WEIGHT       0x0002
#define NGX_HTTP_UPSTREAM_MAX_FAILS    0x0004
#define NGX_HTTP_UPSTREAM_FAIL_TIMEOUT 0x0008
#define NGX_HTTP_UPSTREAM_DOWN         0x0010
#define NGX_HTTP_UPSTREAM_BACKUP       0x0020
#define NGX_HTTP_UPSTREAM_MAX_CONNS    0x0100
#define NGX_HTTP_UPSTREAM_ID           0x0040
#if (T_NGX_HTTP_UPSTREAM_RANDOM)
#define T_NGX_HTTP_UPSTREAM_RANDOM_FLAG 0x0200
#endif


struct ngx_http_upstream_srv_conf_s
{
#if (NGX_HTTP_UPSTREAM_RBTREE)
    ngx_rbtree_node_t node;
#endif
    ngx_http_upstream_peer_t peer;
    void                   **srv_conf;

    ngx_array_t *servers; /* ngx_http_upstream_server_t */

#if (NGX_HTTP_UPSTREAM_HASH)
    ngx_array_t *values;
    ngx_array_t *lengths;
    ngx_uint_t   retries;
#endif

    ngx_uint_t flags;
    ngx_str_t  host;
    u_char    *file_name;
    ngx_uint_t line;
    in_port_t  port;
    ngx_uint_t no_port; /* unsigned no_port:1 */

#if (NGX_HTTP_UPSTREAM_ZONE)
    ngx_shm_zone_t *shm_zone;
#endif
};


typedef struct
{
    ngx_addr_t               *addr;
    ngx_http_complex_value_t *value;
#if (NGX_HAVE_TRANSPARENT_PROXY)
    ngx_uint_t transparent; /* unsigned  transparent:1; */
#endif
} ngx_http_upstream_local_t;


typedef struct
{
    ngx_http_upstream_srv_conf_t *upstream;

    ngx_msec_t connect_timeout;
    ngx_msec_t send_timeout;
    ngx_msec_t read_timeout;
    ngx_msec_t next_upstream_timeout;

    size_t send_lowat;
    size_t buffer_size;
    size_t limit_rate;

    size_t busy_buffers_size;
    size_t max_temp_file_size;
    size_t temp_file_write_size;

    size_t busy_buffers_size_conf;
    size_t max_temp_file_size_conf;
    size_t temp_file_write_size_conf;

    ngx_bufs_t bufs;

    ngx_uint_t ignore_headers;
    ngx_uint_t next_upstream;
    ngx_uint_t store_access;
    ngx_uint_t next_upstream_tries;
    ngx_flag_t buffering;
    ngx_flag_t request_buffering;
    ngx_flag_t pass_request_headers;
    ngx_flag_t pass_request_body;

    ngx_flag_t ignore_client_abort;
    ngx_flag_t intercept_errors;
    ngx_flag_t cyclic_temp_file;
    ngx_flag_t force_ranges;

    ngx_path_t *temp_path;

    ngx_hash_t   hide_headers_hash;
    ngx_array_t *hide_headers;
    ngx_array_t *pass_headers;

    ngx_http_upstream_local_t *local;
    ngx_flag_t                 socket_keepalive;

#if (NGX_HTTP_CACHE)
    ngx_shm_zone_t           *cache_zone;
    ngx_http_complex_value_t *cache_value;

    ngx_uint_t cache_min_uses;
    ngx_uint_t cache_use_stale;
    ngx_uint_t cache_methods;

    off_t cache_max_range_offset;

    ngx_flag_t cache_lock;
    ngx_msec_t cache_lock_timeout;
    ngx_msec_t cache_lock_age;

    ngx_flag_t cache_revalidate;
    ngx_flag_t cache_convert_head;
    ngx_flag_t cache_background_update;

    ngx_array_t *cache_valid;
    ngx_array_t *cache_bypass;
    ngx_array_t *cache_purge;
    ngx_array_t *no_cache;
#endif

    ngx_array_t *store_lengths;
    ngx_array_t *store_values;

#if (NGX_HTTP_CACHE)
    signed cache : 2;
#endif
    signed   store            : 2;
    unsigned intercept_404    : 1;
    unsigned change_buffering : 1;
    unsigned pass_trailers    : 1;
    unsigned preserve_output  : 1;

#if (NGX_HTTP_SSL || NGX_COMPAT)
    ngx_ssl_t *ssl;
    ngx_flag_t ssl_session_reuse;

    ngx_http_complex_value_t *ssl_name;
    ngx_flag_t                ssl_server_name;
    ngx_flag_t                ssl_verify;
#endif

    ngx_str_t module;

    NGX_COMPAT_BEGIN(2)
    NGX_COMPAT_END
} ngx_http_upstream_conf_t;


typedef struct
{
    ngx_str_t                  name;
    ngx_http_header_handler_pt handler;
    ngx_uint_t                 offset;
    ngx_http_header_handler_pt copy_handler;
    ngx_uint_t                 conf;
    ngx_uint_t                 redirect; /* unsigned   redirect:1; */
} ngx_http_upstream_header_t;


typedef struct
{
    ngx_list_t headers;
    ngx_list_t trailers;

    ngx_uint_t status_n;
    ngx_str_t  status_line;

    ngx_table_elt_t *status;
    ngx_table_elt_t *date;
    ngx_table_elt_t *server;
    ngx_table_elt_t *connection;

    ngx_table_elt_t *expires;
    ngx_table_elt_t *etag;
    ngx_table_elt_t *x_accel_expires;
    ngx_table_elt_t *x_accel_redirect;
    ngx_table_elt_t *x_accel_limit_rate;

    ngx_table_elt_t *content_type;
    ngx_table_elt_t *content_length;

    ngx_table_elt_t *last_modified;
    ngx_table_elt_t *location;
    ngx_table_elt_t *accept_ranges;
    ngx_table_elt_t *www_authenticate;
    ngx_table_elt_t *transfer_encoding;
    ngx_table_elt_t *vary;

#if (NGX_HTTP_GZIP)
    ngx_table_elt_t *content_encoding;
#endif

    ngx_array_t cache_control;
    ngx_array_t cookies;

    off_t  content_length_n;
    time_t last_modified_time;

    unsigned connection_close : 1;
    unsigned chunked          : 1;
} ngx_http_upstream_headers_in_t;


typedef struct
{
    ngx_str_t  host;
    in_port_t  port;
    ngx_uint_t no_port; /* unsigned no_port:1 */

    ngx_uint_t           naddrs;
    ngx_resolver_addr_t *addrs;

    struct sockaddr *sockaddr;
    socklen_t        socklen;
    ngx_str_t        name;

    ngx_resolver_ctx_t *ctx;
} ngx_http_upstream_resolved_t;


typedef void (*ngx_http_upstream_handler_pt)(ngx_http_request_t  *r,
                                             ngx_http_upstream_t *u);


struct ngx_http_upstream_s
{
    ngx_http_upstream_handler_pt read_event_handler;
    ngx_http_upstream_handler_pt write_event_handler;

    ngx_peer_connection_t peer;

    ngx_event_pipe_t *pipe;

    ngx_chain_t *request_bufs;

    ngx_output_chain_ctx_t output;
    ngx_chain_writer_ctx_t writer;

    ngx_http_upstream_conf_t     *conf;
    ngx_http_upstream_srv_conf_t *upstream;
#if (NGX_HTTP_CACHE)
    ngx_array_t *caches;
#endif

    ngx_http_upstream_headers_in_t headers_in;

    ngx_http_upstream_resolved_t *resolved;
#if (T_NGX_HTTP_DYNAMIC_RESOLVE)
    ngx_resolver_ctx_t *dyn_resolve_ctx;
#endif

    ngx_buf_t from_client;

    ngx_buf_t buffer;
    off_t     length;

    ngx_chain_t *out_bufs;
    ngx_chain_t *busy_bufs;
    ngx_chain_t *free_bufs;

    ngx_int_t (*input_filter_init)(void *data);
    ngx_int_t (*input_filter)(void *data, ssize_t bytes);
    void *input_filter_ctx;

#if (NGX_HTTP_CACHE)
    ngx_int_t (*create_key)(ngx_http_request_t *r);
#endif
    ngx_int_t (*create_request)(ngx_http_request_t *r);
    ngx_int_t (*reinit_request)(ngx_http_request_t *r);
    ngx_int_t (*process_header)(ngx_http_request_t *r);
    void (*abort_request)(ngx_http_request_t *r);
    void (*finalize_request)(ngx_http_request_t *r, ngx_int_t rc);
    ngx_int_t (*rewrite_redirect)(ngx_http_request_t *r, ngx_table_elt_t *h,
                                  size_t prefix);
    ngx_int_t (*rewrite_cookie)(ngx_http_request_t *r, ngx_table_elt_t *h);

    ngx_msec_t start_time;

    ngx_http_upstream_state_t *state;

    ngx_str_t method;
    ngx_str_t schema;
    ngx_str_t uri;

#if (NGX_HTTP_SSL || NGX_COMPAT)
    ngx_str_t ssl_name;
#endif

    ngx_http_cleanup_pt *cleanup;

    unsigned store     : 1;
    unsigned cacheable : 1;
    unsigned accel     : 1;
    unsigned ssl       : 1;
#if (NGX_HTTP_CACHE)
    unsigned cache_status : 3;
#endif

    unsigned buffering : 1;
    unsigned keepalive : 1;
    unsigned upgrade   : 1;

    unsigned request_sent         : 1;
    unsigned request_body_sent    : 1;
    unsigned request_body_blocked : 1;
    unsigned header_sent          : 1;

#if (T_NGX_MULTI_UPSTREAM)
    unsigned    multi : 1;
    void       *multi_init;
    ngx_pool_t *send_pool;
    ngx_flag_t  multi_mode;
#endif
};


typedef struct
{
    ngx_uint_t status;
    ngx_uint_t mask;
} ngx_http_upstream_next_t;


typedef struct
{
    ngx_str_t  key;
    ngx_str_t  value;
    ngx_uint_t skip_empty;
} ngx_http_upstream_param_t;


ngx_int_t ngx_http_upstream_create(ngx_http_request_t *r);
void      ngx_http_upstream_init(ngx_http_request_t *r);
ngx_http_upstream_srv_conf_t           *
ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags);
char     *ngx_http_upstream_bind_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
                                          void *conf);
char     *ngx_http_upstream_param_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
                                           void *conf);
ngx_int_t ngx_http_upstream_hide_headers_hash(ngx_conf_t               *cf,
                                              ngx_http_upstream_conf_t *conf,
                                              ngx_http_upstream_conf_t *prev,
                                              ngx_str_t *default_hide_headers,
                                              ngx_hash_init_t *hash);


#define ngx_http_conf_upstream_srv_conf(uscf, module) \
    uscf->srv_conf[module.ctx_index]

#if (NGX_HTTP_UPSTREAM_CHECK)

ngx_uint_t ngx_http_upstream_check_add_peer(ngx_conf_t                   *cf,
                                            ngx_http_upstream_srv_conf_t *us,
                                            ngx_addr_t                   *peer);

ngx_uint_t ngx_http_upstream_check_peer_down(ngx_uint_t index);
ngx_uint_t ngx_http_upstream_check_upstream_down(ngx_str_t *upstream);

void ngx_http_upstream_check_get_peer(ngx_uint_t index);
void ngx_http_upstream_check_free_peer(ngx_uint_t index);

ngx_uint_t ngx_http_upstream_check_add_dynamic_peer(
    ngx_pool_t *pool, ngx_http_upstream_srv_conf_t *us, ngx_addr_t *peer);
void ngx_http_upstream_check_delete_dynamic_peer(ngx_str_t  *name,
                                                 ngx_addr_t *peer_addr);

#endif

extern ngx_module_t       ngx_http_upstream_module;
extern ngx_conf_bitmask_t ngx_http_upstream_cache_method_mask[];
extern ngx_conf_bitmask_t ngx_http_upstream_ignore_headers_masks[];


#endif /* _NGX_HTTP_UPSTREAM_H_INCLUDED_ */
